// PrepWindow.cpp - MARIN MSG Anneke Sicherer-Roetman 20081202

#include <stdlib.h> // exit
#include <math.h>   // atan
#include <stdarg.h>
#include <vector>
#include "PrepWindow.h"
#include "preppath.h" // for callbacks
#include "assertplus.h"

/// pointer to control window (needed by callback functions)
PrepWindow *pWindow = NULL;

// callback functions
extern "C"
{

/// closes application    
void on_preppathControlWindow_destroy(GtkObject *object, gpointer user_data) //
{
    quitPathDefinition();
}

/// closes application
void on_quitButton_clicked(GtkObject *object, gpointer user_data)
{
    quitPathDefinition();
}

/// save path in test file
void on_saveButton_clicked(GtkObject *object, gpointer user_data)
{
    savePathDefinition();
}

/// changes current controlled ship
void on_shipSpinner_value_changed(GtkObject *object, gpointer user_data) //
{
    pWindow->setShip(int(gtk_spin_button_get_value(GTK_SPIN_BUTTON(object))) - 1);
    int ship = pWindow->getShip();
    pWindow->setWaypointLabel("%d", getNumberOfWaypoints(ship));
    selectShip(pWindow->getShip());
};


/// changes z scale of plotter
void on_zscaleSlider_value_changed(GtkObject* object, gpointer)
{
    float zscale = gtk_range_get_value(GTK_RANGE(object));
    setZscale(zscale);
}


/// starts path definition action
void on_clearButton_clicked(GtkObject *object, gpointer user_data)
{
    clearPathDefinition(pWindow->getShip());
}


/// rereads path from file
void on_refreshButton_clicked(GtkObject *object, gpointer user_data)
{
    refreshPathDefinition();
}

} // extern "C"


/// constructor
PrepWindow::PrepWindow(int *argc,              ///< from main
                       char ***argv,           ///< from main
                       int nrPulses)           ///< number of waypoints of pulse 0
    : GtkAppWindow(argc, argv, "prepWindow.xml", "preppathControlWindow")
    , m_pWaypointLabel(NULL)
    , m_pShipSpinner(NULL)
    , m_ship(0)
{
    ASSERT(!pWindow, "only one PrepWindow may be used because of callback mechanism");
    pWindow = this;

    // set pointers to selected widgets to be able to control them
    m_pWaypointLabel  = GTK_LABEL      (gtk_builder_get_object (m_pBuilder, "waypointLabel"));
    m_pShipSpinner    = GTK_SPIN_BUTTON(gtk_builder_get_object (m_pBuilder, "shipSpinner"));
    
    // set initial values for widgets
    gtk_spin_button_set_range(m_pShipSpinner, nrPulses ? 1 : 0, nrPulses);
    setWaypointLabel("%d", getNumberOfWaypoints(0));
}
   

/// destructor
PrepWindow::~PrepWindow()
{
    pWindow = 0;
}


/// displays number of waypoints
void PrepWindow::setWaypointLabel(const char *format, ...)
{
    ASSERTPOINTER(pWindow);
    va_list args;
    char buffer[100]; // dangerous!
    va_start(args, format);
    vsprintf(buffer, format, args);
    va_end(args);
    gtk_label_set_text(m_pWaypointLabel, buffer);
}


/// selects controlled pulse
void PrepWindow::setShip(int s)
{
    ASSERTPOINTER(pWindow);
    m_ship = s;   
}
    

/// returns controlled pulse
int PrepWindow::getShip() const
{
    ASSERTPOINTER(pWindow);
    return m_ship;
}
    

/// sets number of pulses
void PrepWindow::setNumberOfShips(int s)
{
    ASSERTPOINTER(pWindow);
    gtk_spin_button_set_range(m_pShipSpinner, s ? 1 : 0, s);
}
    

